package com.dingqb.socket;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class SocketClient {

    public static void main(String[] args) {
        new SocketClient().doSend();
    }

    public void doSend() {
        new Thread(new Client()).start();
    }

    class Client implements Runnable {
        @Override
        public void run() {
            while (true) {
                Socket socket = null;
                DataOutputStream doc = null;
                DataInputStream in = null;
                try {
                    socket = new Socket("127.0.0.1", 9999);
                    OutputStream netOut = socket.getOutputStream();
                    doc = new DataOutputStream(netOut);
                    in = new DataInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Scanner scanner = new Scanner(System.in);
                System.out.println("请输入：");
                String next = scanner.nextLine();
                try {
                    doc.writeUTF("kata:" + next);
                    String res = in.readUTF();
                    System.out.println(res);
                    System.err.println("end socket");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    doc.close();
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}